#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "public.h"
#include "tilt-imu.h"


void * read_imu_tilt_thread(void * parameter)
{
    char r_buf[16];
    int  fd;
    int  ret_size;
	int  r_size = -1;

    memset(r_buf, 0, sizeof(r_buf));

    fd = open(PIPE_IMU, O_RDONLY|O_NONBLOCK, 0);
    if(fd == -1) {
        printf("open for read pipe [%s] error!!!\n", PIPE_IMU);
        exit(1); 
    }

    while(1) {         
        memset(r_buf, 0, sizeof(r_buf));

        ret_size = read(fd, r_buf, r_size);
        if(ret_size == -1)
            if(errno == EAGAIN)
                printf("no data avlaible\n");

        printf("real read bytes %d:%s\n",ret_size, r_buf);

		if (ret_size > 2) {
			strncpy(pk.angle, r_buf, ret_size);
		}
		
        msleep(800);
    }   

    pause();
    unlink(PIPE_IMU);
}
